package com.ruoyi.system.service.impl;

import java.util.List;
import java.util.Optional;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.Stocklist;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.ScreenstockMapper;
import com.ruoyi.system.domain.Screenstock;
import com.ruoyi.system.service.IScreenstockService;
import com.ruoyi.common.core.text.Convert;

/**
 * 每日股票筛选Service业务层处理
 * 
 * @author ruoyi
 * @date 2021-12-31
 */
@Service
public class ScreenstockServiceImpl implements IScreenstockService 
{
    @Autowired
    private ScreenstockMapper screenstockMapper;

    /**
     * 查询每日股票筛选
     * 
     * @param id 每日股票筛选主键
     * @return 每日股票筛选
     */
    @Override
    public Screenstock selectScreenstockById(Long id)
    {
        return screenstockMapper.selectScreenstockById(id);
    }

    @Override
    public Screenstock getOne(Screenstock screenstock) {
        return Optional.ofNullable(this.selectScreenstockList(screenstock)).map(stocklists ->{
            if (stocklists.size() > 0) {
                return stocklists.get(0);
            }
            return null;
        }).orElse(null);
    }

    /**
     * 查询每日股票筛选列表
     * 
     * @param screenstock 每日股票筛选
     * @return 每日股票筛选
     */
    @Override
    public List<Screenstock> selectScreenstockList(Screenstock screenstock)
    {
        return screenstockMapper.selectScreenstockList(screenstock);
    }

    /**
     * 新增每日股票筛选
     * 
     * @param screenstock 每日股票筛选
     * @return 结果
     */
    @Override
    public int insertScreenstock(Screenstock screenstock)
    {
        return screenstockMapper.insertScreenstock(screenstock);
    }

    /**
     * 修改每日股票筛选
     * 
     * @param screenstock 每日股票筛选
     * @return 结果
     */
    @Override
    public int updateScreenstock(Screenstock screenstock)
    {
        return screenstockMapper.updateScreenstock(screenstock);
    }

    /**
     * 批量删除每日股票筛选
     * 
     * @param ids 需要删除的每日股票筛选主键
     * @return 结果
     */
    @Override
    public int deleteScreenstockByIds(String ids)
    {
        return screenstockMapper.deleteScreenstockByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除每日股票筛选信息
     * 
     * @param id 每日股票筛选主键
     * @return 结果
     */
    @Override
    public int deleteScreenstockById(Long id)
    {
        return screenstockMapper.deleteScreenstockById(id);
    }

    @Override
    public void saveScreen(List<Stocklist> list,int... a) {
        StringBuffer stocks =new StringBuffer();
        boolean b=true;
        for (Stocklist stocklist : list) {
            if (b){ stocks.append(stocklist.getSymbol());
                b=false;
            }else {
                stocks.append(","+stocklist.getSymbol());

            }
        }
        Screenstock screenstock = null;
        if (a == null){
            screenstock=new Screenstock();
            screenstock.setDate(DateUtils.parseDate(DateUtils.datePath()));
            screenstock = getOne(screenstock);
        }
        if (screenstock==null){
            screenstock=new Screenstock();
            screenstock.setDate(DateUtils.parseDate(DateUtils.datePath()));
            screenstock.setStocks(stocks.toString());
            insertScreenstock(screenstock);
            return;
        }
        screenstock.setStocks(stocks.toString());
        updateScreenstock(screenstock);
    }
}
